/**
 * 应用统一配置文件
 * @description 整合所有配置项，提供统一的配置管理
 * @author AI Assistant
 * @date 2025-08-18
 */

// ================================
// 环境配置
// ================================

/**
 * 获取环境配置
 * @returns 环境相关配置
 */
export const getEnvironmentConfig = () => {
    // #ifdef H5
    return {
        PLATFORM: 'h5' as const,
        SUPPORT_WEBP: true,
        SUPPORT_INTERSECTION_OBSERVER: true,
        SUPPORT_PERFORMANCE_API: true
    }
    // #endif

    // #ifdef MP-WEIXIN
    return {
        PLATFORM: 'mp-weixin' as const,
        SUPPORT_WEBP: false,
        SUPPORT_INTERSECTION_OBSERVER: false,
        SUPPORT_PERFORMANCE_API: false
    }
    // #endif

    // #ifdef APP-PLUS
    return {
        PLATFORM: 'app-plus' as const,
        SUPPORT_WEBP: true,
        SUPPORT_INTERSECTION_OBSERVER: false,
        SUPPORT_PERFORMANCE_API: false
    }
    // #endif

    // 默认返回值（确保总是有返回值）
    return {
        PLATFORM: 'unknown' as const,
        SUPPORT_WEBP: false,
        SUPPORT_INTERSECTION_OBSERVER: false,
        SUPPORT_PERFORMANCE_API: false
    }
}

// ================================
// 基础配置
// ================================

/**
 * 应用基础配置
 */
export const APP_CONFIG = {
    // 应用信息
    APP_NAME: 'UniApp Base',
    APP_VERSION: '1.0.0',
    APP_DESCRIPTION: 'UniApp基础模板',

    // API配置
    API: {
        BASE_URL: import.meta.env.VITE_APP_BASE_URL || '',
        BASE_PRE: import.meta.env.VITE_APP_BASE_PRE || '',
        TIMEOUT: 10000,
        MAX_RETRIES: 2,
        RETRY_DELAY: 1000,
        CACHE_TIME: 300, // 秒
        CONCURRENT_LIMIT: 5
    },

    // 存储配置
    STORAGE: {
        PREFIX: 'uniapp_base_',
        TOKEN_KEY: 'token',
        USER_INFO_KEY: 'user_info',
        SETTINGS_KEY: 'settings',
        CACHE_TIME: 7 * 24 * 60 * 60 * 1000 // 7天
    },

    // 页面配置
    PAGE: {
        DEFAULT_TITLE: 'UniApp Base',
        LOADING_TEXT: '加载中...',
        EMPTY_TEXT: '暂无数据',
        ERROR_TEXT: '加载失败',
        RETRY_TEXT: '重试'
    }
}

// ================================
// 性能配置
// ================================

/**
 * 性能优化配置
 */
export const PERFORMANCE_CONFIG = {
    // 图片配置
    IMAGE: {
        QUALITY: 80,
        ENABLE_WEBP: getEnvironmentConfig().SUPPORT_WEBP,
        CACHE_TIME: 3600, // 秒
        MAX_RETRIES: 3,
        LAZY_THRESHOLD: 0.1,
        PRELOAD_COUNT: 5, // 预加载图片数量
        COMPRESS_THRESHOLD: 1024 * 1024 // 1MB以上压缩
    },

    // 请求配置
    REQUEST: {
        TIMEOUT: APP_CONFIG.API.TIMEOUT,
        MAX_RETRIES: APP_CONFIG.API.MAX_RETRIES,
        RETRY_DELAY: APP_CONFIG.API.RETRY_DELAY,
        CACHE_TIME: APP_CONFIG.API.CACHE_TIME,
        CONCURRENT_LIMIT: APP_CONFIG.API.CONCURRENT_LIMIT,
        ENABLE_CACHE: true,
        ENABLE_RETRY: true,
        ENABLE_LOADING: true
    },

    // 虚拟列表配置
    VIRTUAL_LIST: {
        ITEM_HEIGHT: 100,
        OVERSCAN: 5,
        CONTAINER_HEIGHT: 600,
        BUFFER_SIZE: 10,
        ENABLE_DYNAMIC_HEIGHT: false
    },

    // 懒加载配置
    LAZY_LOAD: {
        THRESHOLD: 0.1,
        ROOT_MARGIN: '50px',
        ENABLE_INTERSECTION_OBSERVER: getEnvironmentConfig().SUPPORT_INTERSECTION_OBSERVER,
        FALLBACK_DELAY: 100 // 降级方案延迟
    },

    // 监控配置
    MONITOR: {
        ENABLE_FPS: import.meta.env.MODE === 'development',
        ENABLE_MEMORY: import.meta.env.MODE === 'development',
        ENABLE_NETWORK: import.meta.env.MODE === 'development',
        ENABLE_ERROR: true,
        REPORT_INTERVAL: 5000,
        FPS_THRESHOLD: 30,
        MEMORY_THRESHOLD: 100, // MB
        LOAD_TIME_THRESHOLD: 3000, // ms
        ERROR_REPORT_URL: '/api/error/report'
    },

    // 缓存配置
    CACHE: {
        MEMORY_CACHE_SIZE: 100, // 内存缓存项数量
        MEMORY_CACHE_TTL: 3600000, // 1小时
        STORAGE_CACHE_TTL: APP_CONFIG.STORAGE.CACHE_TIME,
        AUTO_CLEANUP: true,
        CLEANUP_INTERVAL: 300000 // 5分钟清理一次
    }
}

// ================================
// UI配置
// ================================

/**
 * UI相关配置
 */
export const UI_CONFIG = {
    // 主题配置
    THEME: {
        PRIMARY_COLOR: '#007AFF',
        SUCCESS_COLOR: '#4CD964',
        WARNING_COLOR: '#FF9500',
        ERROR_COLOR: '#FF3B30',
        INFO_COLOR: '#5AC8FA',
        BACKGROUND_COLOR: '#F5F5F5',
        TEXT_COLOR: '#333333',
        BORDER_COLOR: '#E5E5E5'
    },

    // 动画配置
    ANIMATION: {
        DURATION: 300,
        EASING: 'ease-in-out',
        FADE_DURATION: 200,
        SLIDE_DURATION: 250,
        BOUNCE_DURATION: 400
    },

    // 组件配置
    COMPONENT: {
        // 头部配置
        HEADER: {
            HEIGHT: 44,
            BACKGROUND_COLOR: '#FFFFFF',
            TEXT_COLOR: '#333333',
            SHOW_SHADOW: true,
            SHOW_BACK_BUTTON: true
        },

        // 列表配置
        LIST: {
            ITEM_HEIGHT: 60,
            SEPARATOR_COLOR: '#E5E5E5',
            REFRESH_THRESHOLD: 50,
            LOAD_MORE_THRESHOLD: 100,
            EMPTY_HEIGHT: 200
        },

        // 图片配置
        IMAGE: {
            DEFAULT_SIZE: '80rpx',
            BORDER_RADIUS: '4rpx',
            PLACEHOLDER_COLOR: '#F5F5F5',
            ERROR_COLOR: '#CCCCCC',
            FADE_SHOW: true,
            LAZY_LOAD: true
        },

        // 表单配置
        FORM: {
            INPUT_HEIGHT: 44,
            BUTTON_HEIGHT: 44,
            BORDER_RADIUS: 4,
            LABEL_WIDTH: 80,
            ERROR_COLOR: '#FF3B30'
        }
    },

    // 弹窗配置
    MODAL: {
        MASK_OPACITY: 0.5,
        ANIMATION_DURATION: 300,
        AUTO_CLOSE_DELAY: 2000,
        MAX_WIDTH: 300,
        BORDER_RADIUS: 8
    },

    // Toast配置
    TOAST: {
        DURATION: 2000,
        POSITION: 'center' as 'top' | 'center' | 'bottom',
        MAX_WIDTH: 200,
        BACKGROUND_COLOR: 'rgba(0, 0, 0, 0.8)',
        TEXT_COLOR: '#FFFFFF'
    }
}

// ================================
// 业务配置
// ================================

/**
 * 业务相关配置
 */
export const BUSINESS_CONFIG = {
    // 用户配置
    USER: {
        DEFAULT_AVATAR: '/static/images/default-avatar.png',
        AVATAR_SIZE: 80,
        LOGIN_EXPIRE_TIME: 7 * 24 * 60 * 60 * 1000, // 7天
        AUTO_LOGIN: true,
        REMEMBER_PASSWORD: false
    },

    // 文件上传配置
    UPLOAD: {
        MAX_FILE_SIZE: 10 * 1024 * 1024, // 10MB
        CHUNK_SIZE: 1024 * 1024, // 1MB
        CONCURRENT_UPLOADS: 3,
        ALLOWED_TYPES: ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
        COMPRESS_QUALITY: 0.8,
        MAX_WIDTH: 1920,
        MAX_HEIGHT: 1080
    },

    // 分页配置
    PAGINATION: {
        DEFAULT_PAGE_SIZE: 20,
        MAX_PAGE_SIZE: 100,
        LOAD_MORE_THRESHOLD: 3 // 距离底部3项时加载更多
    },

    // 搜索配置
    SEARCH: {
        DEBOUNCE_DELAY: 300,
        MIN_KEYWORD_LENGTH: 2,
        MAX_KEYWORD_LENGTH: 50,
        HISTORY_MAX_COUNT: 10,
        ENABLE_HOT_SEARCH: true
    }
}

// ================================
// 开发配置
// ================================

/**
 * 开发相关配置
 */
export const DEV_CONFIG = {
    // 调试配置
    DEBUG: {
        ENABLE_CONSOLE: import.meta.env.MODE === 'development',
        ENABLE_VCONSOLE: false,
        ENABLE_PERFORMANCE_MONITOR: import.meta.env.MODE === 'development',
        LOG_LEVEL: 'info' as 'debug' | 'info' | 'warn' | 'error'
    },

    // 模拟数据配置
    MOCK: {
        ENABLE: false,
        DELAY: 1000,
        SUCCESS_RATE: 0.9 // 90%成功率
    },

    // 热更新配置
    HOT_RELOAD: {
        ENABLE: import.meta.env.MODE === 'development',
        PORT: 3000,
        HOST: 'localhost'
    }
}

// ================================
// 导出统一配置
// ================================

/**
 * 获取完整配置
 * @returns 完整的应用配置
 */
export const getAppConfig = () => {
    const envConfig = getEnvironmentConfig()

    return {
        ENV: envConfig,
        APP: APP_CONFIG,
        PERFORMANCE: PERFORMANCE_CONFIG,
        UI: UI_CONFIG,
        BUSINESS: BUSINESS_CONFIG,
        DEV: DEV_CONFIG
    }
}

/**
 * 获取优化配置
 * @returns 性能优化相关配置
 */
export const getOptimizationConfig = () => {
    const envConfig = getEnvironmentConfig()
    return {
        ...PERFORMANCE_CONFIG,
        ENV: envConfig
    }
}

// 默认导出
export default getAppConfig()
